<template>
	<view class="addFriends">
		<view class="top">
			<view class="" id="containert">
				<view class="avatar">
					<image :src="httpUrl+src" mode="aspectFill"></image>
				</view>
				<view class="textPrice">
					¥{{price}}
				</view>
				<view class="textInfo">
					价格具有时效性，具体请以商详为准
				</view>
				<view class="textTitle">
					{{tit}}
				</view>
				<!-- <uni-title type="h4" :title="tit"></uni-title> -->
				<view class="code">
					<uv-qrcode ref="qrcode" size="160rpx" :value="JSON.stringify({dataType:'productDetail',dataCode:id})"></uv-qrcode>
				</view>
				<view class="textUse">使用宜趣多资APP扫描查看</view>
			</view>
		</view>
		<view class="bot">
			<text @tap="canvasToImage.getImage">分享商品</text>
			<text @tap="canvasToImage.saveImage">保存图片</text>
		</view>

	</view>
</template>

<script module="canvasToImage" lang="renderjs">
	import html2canvas from 'html2canvas';
	export default {
		data() {
			return {}
		},
		methods: {
			getImage(event, ownerInstance) {
				//containert为需要操作的dom节点，也就是定义的id值
				var dom = document.getElementById('containert');
				html2canvas(dom, {
					width: dom.clientWidth, //dom 原始宽度
					height: dom.clientHeight,
					scrollY: 0, // html2canvas默认绘制视图内的页面，需要把scrollY，scrollX设置为0
					scrollX: 0,
					dpi: 300,
					useCORS: true, //支持跨域，但好像没什么用
					allowTaint: false
				}).then(function(canv) {
					let url = canv.toDataURL('image/jpg'); // base64数据
					//getImgToShare回调到主script的函数
					ownerInstance.callMethod('getImgToShare', {
						base64data: url,
					})
				});
			},
			saveImage(event, ownerInstance) {
				//containert为需要操作的dom节点，也就是定义的id值
				var dom = document.getElementById('containert');
				html2canvas(dom, {
					width: dom.clientWidth, //dom 原始宽度
					height: dom.clientHeight,
					scrollY: 0, // html2canvas默认绘制视图内的页面，需要把scrollY，scrollX设置为0
					scrollX: 0,
					scale: 2, // 按比例增加分辨率
					dpi: 300,
					useCORS: true, //支持跨域，但好像没什么用
					allowTaint: false
				}).then(function(canv) {
					let url = canv.toDataURL('image/jpg'); // base64数据
					//getImgToShare回调到主script的函数
					ownerInstance.callMethod('getImgToAdd', {
						base64data: url,
					})
				});
			},
		}
	}
</script>

<script>
	import {
		userSimple
	} from '@/utils/api/userInfo.ts'
	import {
		requestAndroidPermission,
		gotoAppPermissionSetting
	} from '@/js_sdk/wa-permission/permission.js'
	export default {
		props: ['src', 'id', 'tit', 'price'],
		data() {
			return {
				httpUrl: getApp().globalData.baseUrl + '/app/file/viewFile?fileId=',
				code: "",
				userData: {},
				url: ''
			}
		},
		onLoad() {
			userSimple().then(res => {
				this.code = 'http://im.yiquduozi.top/#/?shareCode=' + res.data.inviteCode;
				this.userData = res.data
			})
		},
		methods: {
			async getImgToAdd(data) {
				var result = await requestAndroidPermission('android.permission.READ_EXTERNAL_STORAGE')
				console.log(result, 'result')
				if (result == -1) {
					uni.showModal({
						title: '提示',
						content: '需要保存图片的权限，请前往设置开启',
						success: res => {
							if (res.confirm) {
								// 根据平台调用相应的设置页面
								if (uni.getSystemInfoSync()
									.platform === 'android') {
									gotoAppPermissionSetting();
								} else {
									uni.showToast({
										title: '请手动前往设置开启权限',
										icon: 'none'
									});
								}
							}
						}
					});
					return;
				}
				let that = this
				let base64data = data.base64data;
				let bitmap = new plus.nativeObj.Bitmap("test");
				bitmap.loadBase64Data(base64data, () => {
					let url = '_downloads/yflPic' + Date.now() + '.png';
					console.log(url, 'url')
					this.url = url;
					bitmap.save(url, {}, (i) => {
						uni.saveImageToPhotosAlbum({
							filePath: i.target,
							success: function() {
								uni.hideLoading()
								uni.showToast({
									title: '图片保存成功',
									icon: 'none'
								})
								bitmap.clear()
							},
							fail: () => {
								bitmap.clear()
							}
						});
					}, function(e) {
						bitmap.clear();
					});
				}, function() {
					bitmap.clear();
				});
			},
			getImgToShare(data) {
				let that = this
				let base64data = data.base64data;
				let bitmap = new plus.nativeObj.Bitmap("test");
				bitmap.loadBase64Data(base64data, () => {
					let url = '_downloads/yflPic' + Date.now() + '.png';
					console.log(url, 'url')
					this.url = url;
					bitmap.save(url, {}, (i) => {
						uni.share({
							provider: "weixin",
							scene: "WXSceneSession",
							type: 2,
							imageUrl: i.target,
							success: function(res) {
								console.log("success:" + JSON.stringify(res));
							},
							fail: function(err) {
								console.log("fail:" + JSON.stringify(err));
							}
						});
					}, function(e) {
						bitmap.clear();
					});
				}, function() {
					bitmap.clear();
				});
			},
		}
	}
</script>

<style lang="scss">
	.addFriends {
		display: flex;
		// align-items: center;
		flex-direction: column;
		padding: 50rpx;

		>.top {
			flex: 1;

			>view {
				background: #fff;
				// width: fit-content;
				border-radius: 20rpx;
				padding: 50rpx;
			}

			.textPrice {
				font-size: 36rpx;
				// font-weight: bold;
				color: #FF5233;
			}

			.textInfo {
				font-size: 24rpx;
				color: #b5b5b5;
			}

			.textTitle {
				font-size: 28rpx;
				font-weight: bold;
			}

			.textUse {
				color: #b5b5b5;
				font-size: 25rpx;
				text-align: center;
			}

			.code {
				margin: 20rpx 0 5rpx;
				// width: 100%;
				display: flex;
				justify-content: center;
			}

			.avatar {
				display: flex;
				justify-content: center;

				image {
					border-radius: 20rpx;
					width: 480rpx;
					height: 480rpx;
				}
			}
		}

		>.bot {
			width: 100%;
			display: flex;
			justify-content: center;
			margin-top: 20rpx;

			text {
				display: inline-block;
				width: 45%;
				line-height: 80rpx;
				text-align: center;
				font-size: 32rpx;
				border-radius: 80rpx;
			}

			text:nth-of-type(1) {
				background: #6B64D3;
				color: #fff;
			}

			text:nth-of-type(2) {
				background: #6B64D3;
				color: #fff;
			}
		}
	}
</style>